import api from '../index';
import type { ResponseData, pageParam, PagingData } from '../type';
import type { GoodsItem, StoreInfo, PurchaseGoodsPayData, GoodsTimeCardItem, GoodsSkuSpecificationItem } from '.';

export type UserInfo = {
	code : number;
	email : string;
	isPayword : boolean;
	isPhone : boolean;
	isRepertory : boolean;
	isRepertoryMsg : string;
	levelIcon : string;
	levelName : string;
	levelNumber : number;
	msg : string;
	newUser : boolean;
	nickname : string;
	phone : string;
	photo : string;
	salt : string;
	userId : string;
	userSex ?: string
	userBirthday ?: string
	imInfo : {
		loginname : string;
		nick : string;
		pd5 : string;
		sign : string;
		userThirdId : string;
		userType : 1 | 2;
	};
	chainLevelName ?: string;
	countryArea : string
};

// 用户信息
export const getUserInfo = () => api.post<UserInfo>('customer/api/info');

export type AccountItem = {
	frozenMoney : number;
	id : number;
	isDefault : 'Y' | 'N';
	money : number;
	moneyAll : number;
	name : string;
	status : number;
	strategy : string;
};

// 用户账户列表
export const getAccountList = () => api.get<ResponseData<AccountItem[]>>('customer/api/account');

export interface BankCardItem {
	id : number;
	logo : string;
	bankname : string;
	bankcode : string;
}
// 用户已绑定的银行卡列表
export const getBankCardList = () => api.get<ResponseData<{ rows : BankCardItem[] }>>('customer/api/bank');
// 添加银行卡
export const postAddBindBank = (data : { realname : string; bankcode : string; phone : string; idcard : string; typeid : string }) =>
	api.post('customer/api/bank/bindBank', {
		data
	});
// 删除银行卡
export const postDleBankCard = (id : number) => api.delete(`customer/api/bank/${id}`);

export interface bankItem {
	bankname : string;
	id : number;
	logo : string;
}
// 可绑定的银行列表
export const getBankList = () =>
	api.get<ResponseData<bankItem[]>>('system/api/basics/bankTypeList', {
		custom: {
			isLoading: true
		}
	});
export interface AddressItem {
	createTime : string;
	id : number;
	userId : string;
	name : string;
	phone : string;
	proId : string;
	cityId : string;
	areaId : string;
	townId : string;
	address : string;
	isDefaul : 0 | 1;
	zipCode : null;
	nameExt : string;
	labelName : string;
	longitude : number;
	latitude : number;
	updateAddressTime ?: string;
	countryArea : string
}
// 用户收货地址列表
export const getAddressList = () => api.get<ResponseData<AddressItem[]>>('customer/api/addr');
// 收货地址设置为默认
export const postSettingDefaultAddress = (id : number | string) => api.put(`customer/api/addr/settingDegault/${id}`);
export interface AddressParams {
	id ?: number | string;
	name : string;
	phone : string;
	nameExt : string;
	address : string;
	isDefaul : 0 | 1;
	labelName : string;
	proId : string;
	cityId : string;
	areaId : string;
	townId : string;
	longitude : number;
	latitude : number;
	countryArea : string
}
// 用户收货地址新增
export const postAddAddress = (data : AddressParams) =>
	api.post('customer/api/addr', {
		data
	});
// 用户收货地址编辑
export const putEditAddress = (data : AddressParams) =>
	api.put('customer/api/addr', {
		data
	});
// 删除收货地址
export const postDleAddress = (id : number | string) => api.delete(`customer/api/addr/delete/${id}`);
// 收货地址详情
export const getAddressInfo = (id : number | string) => api.get<ResponseData<AddressItem>>(`customer/api/addr/${id}`);
// 地址拆分
interface SplitAddrResult {
	nameExt : string
	village : string
	latitudes : number
	longitudes : number
}
export const splitAddr = (data : {
	address : string,
	location : string
}) =>
	api.post<ResponseData<SplitAddrResult>>(`customer/api/addr/split`, {
		data
	});

// 商品收藏列表
export const getGoodsCollectList = (data : pageParam) =>
	api.get<ResponseData<GoodsItem[]>>('customer/api/collection/list/1', {
		data
	});
// 店铺收藏列表
export const getStoreCollectList = (data : pageParam) =>
	api.get<ResponseData<StoreInfo[]>>('customer/api/collection/list/2', {
		data
	});
// 取消收藏
export const postDelCollect = (data : { targetId : string | number }, type : 1 | 2) =>
	api.delete(`customer/api/collection/${type}`, {
		data,
		custom: {
			isLoading: true,
			goLogin: true
		}
	});

// 添加收藏
export const postAddCollect = (data : { targetId : string | number }, type : 1 | 2) =>
	api.post(`customer/api/collection/${type}`, {
		data,
		custom: {
			isLoading: true,
			goLogin: true
		}
	});
export interface FootmarkItem {
	createTime : string;
	id : number;
	product : GoodsItem;
	productId : string;
	userId : string;
}

// 足迹列表
export const getFootmarkList = (data : pageParam) =>
	api.get<ResponseData<FootmarkItem[]>>('customer/api/footprint/list', {
		data
	});
// 删除足迹
export const postDelFootmark = (id : string) => api.delete(`customer/api/footprint/${id}`);
// 清空足迹
export const postClearFootmark = () => api.delete('customer/api/footprint/clear');
// 意见反馈
export const postFeedback = (data : { content : string; urls : string }) =>
	api.post('system/api/opinion', {
		data,
		custom: {
			isLoading: true,
			showMsg: true
		}
	});
export interface FundDetailsItem {
	isout : 1 | 0;
	id : number;
	remark : string;
	createTime : string;
	cashmoney : number;
}
// 余额明细
export const getFundDetailsList = (parmas : { strategy : string } & pageParam) => {
	const data : Record<string, any> = { ...parmas };
	delete data.strategy;
	return api.get<ResponseData<FundDetailsItem[], true>>(`customer/api/account/details/${parmas.strategy}`, {
		data
	});
};

// 冻结余额明细
export const getFrozenDetailsList = (parmas : { strategy : string } & pageParam) => {
	const data : Record<string, any> = { ...parmas };
	delete data.strategy;
	return api.get(`customer/api/account/frozen/details/${parmas.strategy}`, {
		data
	});
};
type ExtractParams = {
	payword : string;
	cashmoney : string;
	bankid : number;
	type : 1;
	strategy : string;
	payType : 'YHK' | 'ZFB' | 'WX' | 'SYS'
};
// 提现
export const postExtract = (data : ExtractParams) =>
	api.post('customer/api/cash', {
		data
	});
// 提现规则
export const getExtractRule = () => api.get('system/api/basics/cash/rule/1');
export interface ExtractHistoryItem {
	isout : 1 | 0;
	id : number;
	remark : string;
	createTime : string;
	status : number;
	cashmoney : number;
	scaleModel : number;
	isopen : number;
}
// 提现记录
export const postExtractHistory = (data : pageParam) =>
	api.get<ResponseData<ExtractHistoryItem[], true>>('customer/api/cash', {
		data
	});
export interface OrderCount {
	pendingOrderCount : number; //待付款订单统计
	receivingOrderCount : number; //待发货订单统计
	deliverOrderCount : number; //待收货订单统计
	evaluateOrderCount : number; //待评价订单统计
	refundOrderCount : number; //售后订单统计
}
// 订单统计
export const getOrderCount = () => api.get<ResponseData & OrderCount>('order/api/main/count');

export interface TeamItem {
	id : number;
	createTime : string;
	levelIcon : string;
	levelName : string;
	parentCount : number;
	payMoney : number;
	payMoneyProfit : number;
	payOne : number;
	userEmail ?: string;
	userId : string;
	userLevel : number;
	userName : string;
	userNickname : string;
	userParentid : string;
	userPhone : string;
	userPhoto : string;
}

// 团队列表
export const getTeamList = (data : pageParam) =>
	api.get<ResponseData<PagingData<TeamItem>> & { payMoneyAll : number }>('customer/api/my/team', {
		data
	});
// 获取邀请好友背景图地址、人数统计、生成二维码的地址
export const getInviteData = (data : { url : string; templateId ?: string; formType : string; scene ?: string }) =>
	api.post<ResponseData & { global : { backLogo : string; backState : number; backContent : string }; shareUrl : string; salt : string; shareCount : number }>(
		'customer/api/share/pre',
		{
			data,
			custom: {
				goLogin: true
			}
		}
	);
// 获取邀请好友的统计
export const getInviteDetail = (data : pageParam) =>
	api.get<ResponseData<TeamItem[]>>('customer/api/share/detail', {
		data
	});
// 账户权限
export const getWalletRule = (type : string) =>
	api.get<{
		convert : boolean;
		turn : boolean;
	}>(`customer/api/account/rule/${type}`);

// 账户转账规则
interface TurnExtractRule {
	operationa : number;
	scale : number;
	scaleType : 1 | 2;
}
export const getTurnExtractRule = (type : string) =>
	api.get<{
		rule : TurnExtractRule;
	}>(`customer/api/account/turn/rule/${type}`);
// 账户转账
interface TurnParams {
	payword?: string;
	money: string|number;
	userName: string;
	strategy: string;
	code:string
}
export const postTurn = (data: TurnParams) =>
	api.get<ResponseData&{
		arrivalUser: string,
		arrivalMoney: string,
		arrivalTime: string
	}>('customer/api/account/do/turn', {
		data,
		custom: {
			isLoading: true,
			showMsg: true
		}
	});
// 账户转换规则与可转换的账户列表
interface ConvertExtractRuleItem {
	name : string;
	id : number;
	scale : number;
	scaleType : 1 | 2;
	operationa : number;
	strategy : string;
	targetStrategy : string;
}
export const getConvertExtractRule = (type : string) =>
	api.get<{
		rule : ConvertExtractRuleItem[];
	}>(`customer/api/account/convert/rule/${type}`);
// 账户转换
interface ConvertParams {
	payword ?: string;
	money : string;
	target : string;
	strategy : string;
}
export const postConvert = (data : ConvertParams) =>
	api.get('customer/api/account/do/convert', {
		data,
		custom: {
			isLoading: true,
			showMsg: true
		}
	});

// 充值余额
export const postRecharge = (data : { fromType : string; payId : number; money : string | number }) =>
	api.post<PurchaseGoodsPayData>('customer/api/order/recharge/balance', {
		data
	});
// 账户充值结果查询，h5环境下调用微信支付时使用
export const getRechargeResult = (id : string) =>
	api.get(`customer/api/order/recharge/balance/check/${id}`, {
		custom: {
			isLoading: true
		}
	});

//讨论区分类列表
export const getDiscussClassify = () => api.get('customer/forum/class/getList');
// 讨论区分类对应的列表
export const getDiscussList = (id : number | string, params : any) => api.get(`customer/forum/list/${id}`, { params });
// 讨论区详情
export const getDiscussInfo = (id : number | string) => api.get(`customer/forum/info/${id}`);
// 讨论区详情评论列表
export const getDiscussEvaluation = (id : number | string, data : any) => api.get(`customer/comments/list/${id}`, { data });
// 点赞讨论
export const postAddDiscussLike = (id : number | string) => api.post(`customer/forum/likeIt/${id}`);
// 取消点赞讨论
export const postDelDiscussLike = (id : number | string) => api.delete(`customer/forum/noLike/${id}`);
// 讨论区发布评论
export const postAddDiscussEvaluation = (data : any) =>
	api.post('customer/comments/add', {
		data,
		header: {
			'Content-Type': 'application/json'
		}
	});
// 点赞讨论区评论
export const postAddDiscussEvaluationLike = (id : number | string) => api.post(`customer/comments/likeIt/${id}`);
// 取消讨论区评论
export const postDelDiscussEvaluationLike = (id : number | string) => api.delete(`customer/comments/noLike/${id}`);
// 发布讨论区文章
export const postAddDiscuss = (data : any) =>
	api.post('customer/forum/add', {
		data,
		header: {
			'Content-Type': 'application/json'
		}
	});
export interface UserTimeCardItem extends GoodsTimeCardItem {
	residueSize : number;
}

//用户的次卡列表
export const getUserTimeCardList = (data : { skuId : string; status ?: number }) => api.get<ResponseData<UserTimeCardItem[]>>('merchant/customer/seqcard/list', { data });
export interface TimeCardGoods extends GoodsSkuSpecificationItem {
	productName : string
}
//获取卡的服务列表
export const getUserTimeCard = (data : { carId : string | number }) =>
	api.get<ResponseData<TimeCardGoods[]>>('merchant/customer/seqcard/carId', {
		data,
		custom: {
			isLoading: true
		}
	});
// 转账短信
export const postTurnSms = () =>
	api.post('customer/api/sms/turnSms', {
		custom: {
			auth: false,
			showMsg: true
		}
	}
)